/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MongoDAL;

import DALFactory.IDataSearchLogMapper;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

	


/**
 * 查找日志表（Search_Log）数据操作
 * 
 * @author Administrator
 */
public class MongoDataSearchLogMapper implements IDataSearchLogMapper {
	/*
	 * (non-Javadoc)
	 * 
	 * @see MongoDAL.IDataSearchLogMapper#InsertSearch_Log(java.lang.String,
	 * java.lang.String, java.util.Date, int)
	 */
	@Override
	public boolean InsertSearch_Log(String keyword, String type, Date date) {
		// 新建MongoDB语句
		MongoStatement mstm = new MongoStatement();
		// 参照完整性

		// 插入对象
		DBObject insertValue = new BasicDBObject();

		insertValue.put("keyword", keyword);
		insertValue.put("type", type);
		insertValue.put("Date", date);

		DBCollection Search_LogColl = mstm.QueryTable("Search_Log");
		Search_LogColl.insert(insertValue);
		mstm=null;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see MongoDAL.IDataSearchLogMapper#querySearch_Log(java.lang.String,
	 * java.util.Date, java.util.Date)
	 */
	@Override
	public int querySearchLog(String type, Date startDate, Date endDate)

	{

		// 新建MongoDB语句
		MongoStatement mstm = new MongoStatement();
		// Search_Log集合
		DBCollection Search_LogColl = mstm.QueryTable("Search_Log");
		// 查询文档
		
	
		BasicDBObject queryDBObject = new BasicDBObject();
		queryDBObject.put("type", type);
		DBObject dateRangeDBObject = new BasicDBObject("$gte", startDate)
				.append("$lte", endDate);
		queryDBObject.put("Date", dateRangeDBObject);
		int result = Search_LogColl.find(queryDBObject).count();

		mstm=null;
		return result;
	}

	@Override
	public int querySearchLog(Date startDate, Date endDate)

	{
		// 新建MongoDB语句
		MongoStatement mstm = new MongoStatement();
		// Search_Log集合
		DBCollection Search_LogColl = mstm.QueryTable("Search_Log");
		// 查询文档
		BasicDBObject queryDBObject = new BasicDBObject();
		
		DBObject dateRangeDBObject = new BasicDBObject("$gte",startDate)
				.append("$lte", endDate);
		queryDBObject.put("Date", dateRangeDBObject);
		int result = Search_LogColl.find(queryDBObject).count();
		mstm=null;
		return result;
	}

	public static void main(String[] args) {
		IDataSearchLogMapper mdm4 = new MongoDataSearchLogMapper();
		Date dt = new Date();
		dt.setYear(01);
		
		Date de = new Date();

		de.setYear(113);
		boolean b = mdm4.InsertSearch_Log("Area", "a", new Date());
		System.out.println(mdm4.querySearchLog(dt, de));
	}
}
